<template>
  <div class="product-detail-main">
    <div class="product-detail-container">
      <!-- 左侧图片 -->
      <div class="product-image">
        <img :src="product.image" alt="封面图" v-if="product.image" />
      </div>
      <!-- 右侧信息 -->
      <div class="product-info">
        <h1 class="product-title">{{ product.title }}</h1>
        <div class="product-price">
          <span>价格</span>
          <span class="price-value">￥{{ selectedSku ? selectedSku.price : (skuList[0]?.price ?? product.price) }}</span>
        </div>
        <div class="sku-select-block" v-if="skuList.length">
          <div class="sku-label">规格</div>
          <div class="sku-btn-group">
            <button
              v-for="sku in skuList"
              :key="sku.id"
              :class="['sku-btn', { active: selectedSku && selectedSku.id === sku.id }]"
              @click="selectSku(sku)"
            >
              {{ sku.title }}
            </button>
          </div>
        </div>
        <div class="action-btns">
          <button class="buy-btn">立即购买</button>
          <button class="cart-btn" @click="addToCart">加入购物车</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { getProductDetail, getProductSkuList } from '@/api/business/product.js';
import { saveCartItem } from '@/api/business/cartItem.js';
import { ElMessage } from 'element-plus';

const route = useRoute();
const product = ref({});
const skuList = ref([]);
const selectedSku = ref(null);

onMounted(() => {
  doGetProductDetail();
  doGetProductSkuList();
});

const doGetProductDetail = async () => {
  try {
    const res = await getProductDetail(route.params.id);
    if (res.code === 200) {
      product.value = res.data;
    } else {
      ElMessage.error(res.msg || '获取商品详情失败');
      product.value = {};
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '获取商品详情失败');
    product.value = {};
  }
};

const doGetProductSkuList = async () => {
  try {
    const res = await getProductSkuList(route.params.id);
    if (res.code === 200 && Array.isArray(res.data)) {
      skuList.value = res.data;
      if (skuList.value.length) {
        selectedSku.value = skuList.value[0];
      }
    } else {
      ElMessage.error(res.msg || '获取SKU列表失败');
      skuList.value = [];
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '获取SKU列表失败');
    skuList.value = [];
  }
};

function selectSku(sku) {
  selectedSku.value = sku;
}

// 加入购物车
const addToCart = async () => {
  if (!selectedSku.value) {
    ElMessage.warning('请选择商品规格');
    return;
  }
  try {
    const res = await saveCartItem({
      productSkuId: selectedSku.value.id,
      amount: 1
    });
    if (res.code === 200) {
      ElMessage.success('已加入购物车');
    } else {
      ElMessage.error(res.msg || '加入购物车失败');
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '加入购物车失败');
  }
};
</script>

<style scoped>
.product-detail-main {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 60vh;
  background: #fff;
  padding: 40px 0;
}
.product-detail-container {
  display: flex;
  gap: 48px;
  max-width: 900px;
  width: 100%;
}
.product-image {
  flex: 0 0 360px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.product-image img {
  width: 340px;
  height: 340px;
  object-fit: cover;
  border-radius: 16px;
  box-shadow: 0 2px 12px #eee;
}
.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  min-width: 320px;
}
.product-title {
  font-size: 2.1rem;
  font-weight: bold;
  margin-bottom: 24px;
}
.product-price {
  font-size: 1.1rem;
  margin-bottom: 24px;
  display: flex;
  align-items: baseline;
  gap: 12px;
}
.price-value {
  color: #e02e24;
  font-size: 2rem;
  font-weight: bold;
  margin-left: 8px;
}
.sku-select-block {
  margin-bottom: 32px;
}
.sku-label {
  font-size: 1rem;
  margin-bottom: 8px;
  color: #333;
}
.sku-btn-group {
  display: flex;
  gap: 12px;
}
.sku-btn {
  min-width: 48px;
  padding: 6px 18px;
  border: 1.5px solid #d9d9d9;
  border-radius: 6px;
  background: #fff;
  font-size: 1rem;
  color: #222;
  cursor: pointer;
  transition: border 0.2s, color 0.2s, background 0.2s;
}
.sku-btn.active,
.sku-btn:hover {
  border: 2px solid #e02e24;
  color: #e02e24;
  background: #fff7f7;
}
.action-btns {
  display: flex;
  gap: 18px;
  margin-top: 32px;
}
.buy-btn,
.cart-btn {
  font-size: 1.1rem;
  padding: 12px 32px;
  border-radius: 6px;
  border: 2px solid #e02e24;
  cursor: pointer;
  font-weight: bold;
  transition: background 0.2s, color 0.2s;
}
.buy-btn {
  background: #fff;
  color: #e02e24;
}
.buy-btn:hover {
  background: #ffeaea;
}
.cart-btn {
  background: #e02e24;
  color: #fff;
}
.cart-btn:hover {
  background: #c91d1d;
}
</style> 